package scau.erps.view.freemarket;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import scau.erps.app.Erps;
import scau.erps.entity.FreeMarket;
import scau.erps.service.FreeMarketService;
import scau.erps.service.ResearchService;
import scau.erps.service.impl.FreeMarketServiceImpl;


/**
 * 市场面板模板
 * @author outsider
 *
 */
public abstract class MarketPanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	protected JPanel contentPanel,actionPanel;
	protected JComboBox<String> materialCombo,productCombo,researchCombo;
	protected JButton buyBtn,deleteBtn,refreshBtn,addBtn,filterBtn;
	protected JTable table;
	protected JScrollPane dataPanel;
	protected DefaultTableCellRenderer render;
	protected MarketListModel dataModel;
	protected FreeMarketService freeMarketService=Erps.getIocInstance().get(FreeMarketServiceImpl.class);
	private String type;
	public MarketPanel(String type){
		this.type=type;
		materialCombo = new JComboBox<String>(new String[]{"选择产品","M1","M2","M3","M4"});	
		productCombo = new JComboBox<String>(new String[]{"选择产品","Beryl","Crystal","Ruby","Sapphire"});
		researchCombo =new JComboBox<String>(new String[]{"选择产品","Crystal","Ruby","Sapphire"});
		buyBtn =new JButton("购买");
		filterBtn = new JButton("筛选");
		deleteBtn = new JButton("撤回");
		refreshBtn = new JButton("刷新");
		addBtn = new JButton("发布");
		filterBtn.addActionListener(this);
		buyBtn.addActionListener(this);	
		addBtn.addActionListener(this);
		deleteBtn.addActionListener(this);
		refreshBtn.addActionListener(this);
		actionPanel = new JPanel(); //顶部交互操作面板
		actionPanel.add(materialCombo);
		actionPanel.add(productCombo);
		actionPanel.add(researchCombo);
		actionPanel.add(filterBtn);
		actionPanel.add(addBtn);
		actionPanel.add(deleteBtn);	
		actionPanel.add(refreshBtn);
		actionPanel.add(buyBtn);
		materialCombo.setVisible(false);
		productCombo.setVisible(false);
		researchCombo.setVisible(false);
		table = new JTable();
		table.setPreferredScrollableViewportSize(new Dimension(900, 450));
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		dataPanel = new JScrollPane(table);
		dataPanel.setViewportView(table);
		render = new DefaultTableCellRenderer();   
		render.setHorizontalAlignment(JLabel.CENTER);   
		table.setDefaultRenderer(Object.class,render);
		contentPanel = new JPanel();
  		contentPanel.setLayout(new BorderLayout());
		contentPanel.add(actionPanel,BorderLayout.NORTH);
		contentPanel.add(dataPanel,BorderLayout.CENTER);
		this.add(contentPanel);
		this.setVisible(true);
		this.dataModel = getMarketListModel(type);
		this.table.setModel(dataModel);
		hidden();
	}
	public MarketListModel getMarketListModel(String type){
		
		List<FreeMarket> list = null;
		list=freeMarketService.getFreeMarketByType(type);			
		dataModel = new MarketListModel(list);
		return dataModel;		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==filterBtn){
			final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(dataModel);
			List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
			String material = null;
			String product  = null;
			String research = null;
			if(materialCombo.getSelectedIndex() != 0 ){
				material = (String) materialCombo.getSelectedItem();
			}
			if(productCombo.getSelectedIndex()  != 0 ){
				product = (String) productCombo.getSelectedItem();
			}
			if(researchCombo.getSelectedIndex() != 0){
				research = (String) researchCombo.getSelectedItem();
			}
			if(material !=null){
				filters.add(RowFilter.regexFilter(material));
			}
			if(product!=null){
				filters.add(RowFilter.regexFilter(product));
			}
			if(research!=null){
				filters.add(RowFilter.regexFilter(research));
			}
			sorter.setRowFilter(RowFilter.andFilter(filters));
			this.table.setRowSorter(sorter); 
            this.table.setModel(dataModel);	
            hidden();
		}
		else if(e.getSource()==refreshBtn){
			this.table.setModel(getMarketListModel(type));
			hidden();
		}
		else if(e.getSource()==deleteBtn){
			int row = this.table.getSelectedRow();
			if (row == -1) {
				JOptionPane.showConfirmDialog(this, "未选中任何行", "提示",
						JOptionPane.CLOSED_OPTION);
			} else {
				int teamid=(int)this.table.getValueAt(row, 1);
				if (teamid!=Erps.getCurrentTeam().getId()){
					JOptionPane.showMessageDialog(this, "只能撤回本小组发出的订单", "提示", JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				int id=(int) this.table.getValueAt(row, 0);
				boolean flag=freeMarketService.delete(id);
				if (flag){
					JOptionPane.showMessageDialog(this, "撤回成功", "提示", JOptionPane.INFORMATION_MESSAGE);
				}
				else{
					JOptionPane.showMessageDialog(this, "已被其他小组购买", "提示", JOptionPane.INFORMATION_MESSAGE);
				}
				this.table.setModel(getMarketListModel(type));
				hidden();
			}
		}	
		else if(e.getSource()==buyBtn){
			int row = this.table.getSelectedRow();
			if (row == -1) {
				JOptionPane.showConfirmDialog(this, "未选中任何行", "提示",
						JOptionPane.CLOSED_OPTION);
			} else {
				int teamid=(int)this.table.getValueAt(row, 1);
				if (teamid==Erps.getCurrentTeam().getId()){
					JOptionPane.showMessageDialog(this, "不能购买本小组发布的信息", "提示", JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				int id=(int) this.table.getValueAt(row, 0);
				int flag=freeMarketService.buy(id);
				if (flag==0){
					JOptionPane.showMessageDialog(this, "购买成功，请到工作人员处领取。", "提示", JOptionPane.INFORMATION_MESSAGE);
				}
				if (flag==-1){
					JOptionPane.showMessageDialog(this, "该信息已被撤回。", "提示", JOptionPane.INFORMATION_MESSAGE);
				}
				if (flag==-2){
					JOptionPane.showMessageDialog(this, "已被其他小组购买。", "提示", JOptionPane.INFORMATION_MESSAGE);
				}
				if (flag==-3){
					JOptionPane.showMessageDialog(this, "当前小组现金不足。", "提示", JOptionPane.INFORMATION_MESSAGE);
				}
				if (flag==-4){
					JOptionPane.showMessageDialog(this, "该产品研发已完成，不需要购买。", "提示", JOptionPane.INFORMATION_MESSAGE);
				}
				this.table.setModel(getMarketListModel(type));
				hidden();
			}
		}	
		else if (e.getSource()==addBtn){
			new addDialog(type);
			this.table.setModel(getMarketListModel(type));
			hidden();
		}
	}
	private void hidden(){
		if (type.equals("Material")==false){                   
		TableColumn column = table.getColumnModel().getColumn(3); //不是原材料列表 隐藏第四列（数量）
		column.setMinWidth(0);   
		column.setMaxWidth(0);
		}
	}
}	



